package edu.bbu.commonproject.backend.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class SystemConfig
{
	/**	The mongo host address.*/
	public static final String MONGO_HOST_ADRESS = "mongohostadress";
	
	/**	The mongo host port.*/
	public static final String MONGO_HOST_PORT= "mongohostport";
	
	/**	The thrift port.*/
	public static final String THRIFT_PORT = "thriftport";
	
	/**	The socket port.*/
	public static final String SOCKET_PORT = "socketport";
	
	/**	The properties.*/
	private static Properties properties;
	
	/**	The properties file patch.*/
	private static String propFilePatch = "conf" + File.separator + "system.prop";
	
	static
	{
		properties = new Properties();
		
		File file = new File(propFilePatch);
		
		if(file.exists())
		{
			try
			{
				FileInputStream inputStream = new FileInputStream(file);
				properties.load(inputStream);
				inputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		else 
		{
			createDefault();
			
			try
			{
				FileOutputStream outputStream = new FileOutputStream(file);
				properties.store(outputStream, null);
				outputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Create default properties.
	 */
	public static void createDefault()
	{
		properties.setProperty(MONGO_HOST_ADRESS, "localhost");
		properties.setProperty(MONGO_HOST_PORT, "27017");
		properties.setProperty(THRIFT_PORT, "32767");
		properties.setProperty(SOCKET_PORT, "32768");
	}
	
	/**
	 * Getting the value of a key.
	 * @param key the property key.
	 * @return the value of a property.
	 */
	public static String getValue(String key)
	{
		String value = properties.getProperty(key);
		
		if(value != null)
		{
			return value;
		}
		
		return new String();
	}
}
